﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Linq.Dynamic.Core;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using PMS.XCMG.Entity;
using PMS.XCMG.IBusiness;
using PMS.XCMG.Util;
using PMS.XCMG.Models;

namespace PMS.XCMG.Business
{
    public class MaterielSortingLocationBusiness : BusRepository<MaterielSortingLocation>, IMaterielSortingLocationBusiness, IScopedDependency
    {
        private IServiceProvider SvcProvider { get; set; }
        public MaterielSortingLocationBusiness(GDbContext context, IServiceProvider svcProvider)
            : base(context)
        {
            this.SvcProvider = svcProvider;
        }

        public async Task<PageResult<MaterielSortingLocation>> GetPageResultAsync(PageInput<MaterielSortingLocationQM> query)
        {
            var queryable = this.GetQueryable(true);
            queryable = queryable.Include(i => i.Location).Include(i => i.SortingConfig).Include(i => i.Spec);

            var search = query.Search;
            queryable = queryable.Where(w => w.SortingConfigId == search.SortingConfigId);

            int count = await queryable.CountAsync();
            var list = await queryable.OrderBy(i => i.Location.ElectricCode)
                .Skip((query.PageNo - 1) * query.PageSize)
                .Take(query.PageSize)
                .ToListAsync();

            return new PageResult<MaterielSortingLocation> { Data = list, Total = count, PageNo = query.PageNo };
        }
    }
}
